#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
#include <vector>
using namespace std;

// 提取角度转换
double change_angle(string input_angle)
{
    double out_date;
    int xishu;

    size_t degree_pos = input_angle.find("°");             // 查找°的位置
    size_t minute_pos = input_angle.find("′", degree_pos); // 查找'的位置
    size_t second_pos = input_angle.find("″", minute_pos); // 查找″的位置

    string test_pos = input_angle.substr(0, 1);
    // 提取数字
    if (test_pos == "-")
        xishu = -1;

    else
        xishu = 1;

    string degrees = input_angle.substr(0, degree_pos);                               // 提取°前面的数字
    string minutes = input_angle.substr(degree_pos + 2, minute_pos - degree_pos - 2); // 提取°和'之间的数字
    string seconds = input_angle.substr(minute_pos + 3, second_pos - minute_pos - 3); // 提取'和″之间的数字
    out_date = stod(degrees) + xishu * stod(minutes) / 60.0 + xishu * stod(seconds) / 3600.0;

    // 将秒转换为 double 类型返回，具体实现可以根据需要
    return out_date;
}

// 进行计算
double counter_angle(double T, double h)
{
    double pi = 4 * atan(1);
    double a = 6378137;
    double G_M = 398600441800000;
    double omega = 0.00007292115;
    double f = 1 / 298.257223563;
    double b = 6356752.31414;
    double e_prime_2 = 0.00673949674228;
    double E = sqrt(a * a - b * b);
    double u = b;
    double e_prime = E / b;

    double m = omega * omega * a * a * b / G_M;
    double U0 = G_M * (1 - e_prime_2 / 3 + (e_prime_2 * e_prime_2) / 5) / b + omega * omega * a * a / 3;

    double q0 = 0.5 * ((1 + 3 * b * b / (E * E)) * atan(E / b) - 3 * (b / E));
    double q0_prime = 3 * (1 + u * u / (E * E)) * (1 - (u / E) * atan(E / u)) - 1;
    double gama_a = G_M * (1 - m - (m * e_prime * q0_prime) / (6 * q0)) / (a * b);
    double gama_b = G_M * (1 + m * e_prime * q0_prime / (3 * q0)) / (a * a);
    double phi = T * pi / 180;

    double gama = (a * gama_a * cos(phi) * cos(phi) + b * gama_b * sin(phi) * sin(phi)) / sqrt(a * a * cos(phi) * cos(phi) + b * b * sin(phi) * sin(phi));
    double gama_h = gama * (1 - (2 / a) * (1 + f + m - 2 * f * sin(phi) * sin(phi)) * h + 3 * (h * h) / (a * a));
    return gama_h;
}

// 主函数，程序的入口点
int main()
{
    double h[] = {2000, 2000, 6000, 6000}; // 定义一个包含高度的数组

    vector<double> output_angle; // 存储转换后的角度
    vector<double> G;            // 存储计算出的结果

    string input_angle[] = {"23°12′23″", "-23°12′23″", "23°12′23″", "-23°12′23″"}; // 定义输入角度的字符串数组

    for (int i = 0; i < 4; i++)
    {
        double angle = change_angle(input_angle[i]); // 转换输入角度
        output_angle.push_back(angle);               // 存储转换后的角度
    }

    for (int j = 0; j < output_angle.size(); j++)
    {
        double g = counter_angle(output_angle[j], h[j]); // 根据输出角度和高度计算结果
        G.push_back(g);                                  // 存储计算出的结果
    }

    for (int j = 0; j < G.size(); j++)
    {
        cout << "h:" << int(h[j]) << "  angle:" << setiosflags(ios::fixed) << setprecision(6) << output_angle[j] << "   G:" << setiosflags(ios::fixed) << setprecision(15) << G[j] << endl; // 输出高度、角度和计算结果
    }
}